package com.wxy.controller;

import com.wxy.dao.IdConnectionDao;
import com.wxy.eneity.*;
import com.wxy.service.TableMessageService;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.service.connection.ConnectionDetails;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;
import java.util.Map;

/**
 * @author 47868
 */
@Controller
@RequestMapping("/message")
public class TableMessageController {
    private final TableMessageService tableMessageService;
    private final IdConnectionDao idConnectionDao;

    @Autowired
    public TableMessageController(TableMessageService tableMessageService,
                                  IdConnectionDao idConnectionDao, ConnectionDetails connectionDetails) {
        this.tableMessageService = tableMessageService;
        this.idConnectionDao = idConnectionDao;
    }

    @GetMapping("/getTableStruct/{tableName}")
    @ResponseBody
    public ModelAndView getTableStruct(@PathVariable("tableName") String tableName){
        int id = ConnThreadLocal.CONN.get();
        IdConnection connection = idConnectionDao.selectById(id);
        ModelAndView mv = new ModelAndView();
        mv.addObject("tableName", tableName);
        mv.addObject("tableStructs",tableMessageService.getTableStruct(connection,tableName));
        mv.setViewName("tableMessage");
        return mv;
    }
    @GetMapping("/getMessage/{tableName}")
    @ResponseBody
    public List<List<String>> getMessage(@PathVariable("tableName") String tableName){
        // 去除首尾的引号
        tableName = tableName.replaceAll("^['\"]+|['\"]+$", "");
        int id = ConnThreadLocal.CONN.get();
        IdConnection connection = idConnectionDao.selectById(id);
        return tableMessageService.getMessage(connection,tableName);
    }
    @PostMapping("/addColumn/{tableName}")
    @ResponseBody
    public String addColumn(@PathVariable("tableName") String tableName,@RequestBody NewColumn newColumn){
        tableName = tableName.replaceAll("^['\"]+|['\"]+$", "");
        int id = ConnThreadLocal.CONN.get();
        IdConnection connection = idConnectionDao.selectById(id);
        return tableMessageService.addColumn(connection,tableName,newColumn);
    }
    @DeleteMapping("/deleteColumn/{tableName}/{columnName}")
    @ResponseBody
    public String deleteColumn(@PathVariable("tableName") String tableName,@PathVariable("columnName") String columnName){
        tableName = tableName.replaceAll("^['\"]+|['\"]+$", "");
        int id = ConnThreadLocal.CONN.get();
        IdConnection connection = idConnectionDao.selectById(id);
        return tableMessageService.deleteColumn(connection,tableName,columnName);
    }
    @PostMapping("/renameColumn/{tableName}")
    @ResponseBody
    public String renameColumn(@PathVariable("tableName") String tableName,@RequestBody RenameColumn renameColumn){
        tableName = tableName.replaceAll("^['\"]+|['\"]+$", "");
        int id = ConnThreadLocal.CONN.get();
        IdConnection connection = idConnectionDao.selectById(id);
        return tableMessageService.renameColumn(connection,tableName,renameColumn);
    }
    @PostMapping("/addData/{tableName}")
    @ResponseBody
    public String addData(@PathVariable("tableName") String tableName,
                          @RequestBody Map<String,Object> data){
        tableName = tableName.replaceAll("^['\"]+|['\"]+$", "");
        int id = ConnThreadLocal.CONN.get();
        IdConnection connection = idConnectionDao.selectById(id);
        return tableMessageService.addData(connection,tableName,data);
    }

    @PostMapping("/updateData/{tableName}")
    @ResponseBody
    public String updateData(@PathVariable("tableName") String tableName,@RequestBody Map<String,Object> data){
        tableName = tableName.replaceAll("^['\"]+|['\"]+$", "");
        int id = ConnThreadLocal.CONN.get();
        IdConnection connection = idConnectionDao.selectById(id);
        return tableMessageService.updateData(connection,tableName,data);
    }
    @DeleteMapping("/deleteRow/{tableName}/{type}/{name}/{id}")
    @ResponseBody
    public String deleteRow(@PathVariable("tableName") String tableName,
                            @PathVariable("type") String type,
                            @PathVariable("name") String name,
                            @PathVariable("id") Object uid){
        tableName = tableName.replaceAll("^['\"]+|['\"]+$", "");
        int id = ConnThreadLocal.CONN.get();
        IdConnection connection = idConnectionDao.selectById(id);
        return tableMessageService.deleteRow(connection,tableName,type,name,uid);
    }
    @DeleteMapping("/deleteAllData/{tableName}")
    @ResponseBody
    public String deleteAllData(@PathVariable("tableName") String tableName){
        tableName = tableName.replaceAll("^['\"]+|['\"]+$", "");
        int id = ConnThreadLocal.CONN.get();
        IdConnection connection = idConnectionDao.selectById(id);
        return tableMessageService.deleteAll(connection,tableName);
    }
}
